package com.doudou.casebook.utils;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.doudou.casebook.R;

public class BudgetDialog extends Dialog implements View.OnClickListener {
    ImageView cancelIv;
    EditText moneyEdit;
    Button ensureBtn;
    OnEnsureListener onEnsureListener;

    public void setOnEnsureListener(OnEnsureListener onEnsureListener) {
        this.onEnsureListener = onEnsureListener;
    }

    public BudgetDialog(@NonNull Context context) {
        super(context);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_budget);
        cancelIv = findViewById(R.id.dialog_budget_iv_error);
        moneyEdit = findViewById(R.id.dialog_budget_et);
        ensureBtn = findViewById(R.id.dialog_budget_btn_ensure);

        cancelIv.setOnClickListener(this);
        moneyEdit.setOnClickListener(this);
        ensureBtn.setOnClickListener(this);

    }

    public interface OnEnsureListener {
        public void onEnsure(float money);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.dialog_budget_iv_error) {
            cancel();
        } else if (v.getId() == R.id.dialog_budget_btn_ensure) {
            String editText = moneyEdit.getText().toString();
            if (TextUtils.isEmpty(editText)) {
                Toast.makeText(getContext(), "金额不能为空!", Toast.LENGTH_SHORT).show();
                return;
            }
            if (Float.parseFloat(editText) <= 0) {
                Toast.makeText(getContext(), "金额不能小于0!", Toast.LENGTH_SHORT).show();
                return;
            }

            if (onEnsureListener != null) {
                onEnsureListener.onEnsure(Float.parseFloat(editText));
            }
            cancel();
        }
    }
    //设置dialog尺寸与屏幕尺寸一样宽
    public void setDialogSize(){
        //获取窗口对象
        Window window = getWindow();
        //获取对象参数
        assert window != null;
        WindowManager.LayoutParams wlp = window.getAttributes();
        //获取屏幕宽度
        Display d = window.getWindowManager().getDefaultDisplay();
        wlp.width = d.getWidth();
        wlp.gravity = Gravity.BOTTOM;
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(wlp);
        handler.sendEmptyMessageDelayed(1,100);
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            // 自动弹出软键盘
            InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
        }
    };

}
